home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 701-725 / 708 / intuisup / intuisup42.lha / Intuisup / source.lha / Editor / save.c < prev    next >
C/C++ Source or Header  |  1992-01-09  |  8KB  |  294 lines

  1. /* $Revision Header *** Header built automatically - do not edit! ***********
  2.  *
  3.  *    (C) Copyright 1991 by Torsten Jürgeleit
  4.  *
  5.  *    Name .....: save.c
  6.  *    Created ..: Sunday 22-Dec-91 21:23:04
  7.  *    Revision .: 1
  8.  *
  9.  *    Date        Author                 Comment
  10.  *    =========   ====================   ====================
  11.  *    31-Dec-91   Torsten Jürgeleit      new font management
  12.  *    22-Dec-91   Torsten Jürgeleit      Created this file!
  13.  *
  14.  ****************************************************************************
  15.  *
  16.  *    Save ISUP files
  17.  *
  18.  * $Revision Header ********************************************************/
  19.  
  20.     /* Includes */
  21.  
  22. #include "includes.h"
  23. #include "defines.h"
  24. #include "imports.h"
  25. #include "protos.h"
  26.  
  27.     /* Save project */
  28.  
  29.    SHORT
  30. save_project(VOID)
  31. {
  32.    struct FileRequester  *freq = project_file_requester;
  33.    SHORT status = EDITOR_STATUS_NORMAL;
  34.  
  35.    /* Display ARP file requester and check if user selected cancel */
  36.    IChangeMousePointer(ewin, NULL);
  37.    freq->fr_FuncFlags |= FRF_DoColor;
  38.    freq->fr_Hail       = PROJECT_SAVE_HAIL_TEXT;
  39.    if (FileRequest(freq)) {
  40.       struct TemplateList  *tl = &template_list;
  41.       BPTR  fh;
  42.       BYTE  path[LONG_DSIZE + LONG_FSIZE + 1];
  43.       SHORT len = strlen(freq->fr_File) - 4;
  44.  
  45.       /* Prepare file name and project name */
  46.       if (len < 1 || Strcmp(freq->fr_File + len, ".tpl")) {
  47.      strcat(freq->fr_File, ".tpl");
  48.      len += 4;
  49.       }
  50.       change_project_name(tl, freq->fr_File, len);
  51.  
  52.       /* Build full path for project file */
  53.       strcpy(&path[0], freq->fr_Dir);
  54.       TackOn(&path[0], freq->fr_File);
  55.  
  56.       /* Check if file already exists and inform user */
  57.       if (fh = Open(&path[0], (LONG)MODE_OLDFILE)) {
  58.      Close(fh);
  59.       }
  60.       if (!fh || ok_cancel_requester(" Save ", "File already exists.\\n\\n"
  61.                  "Do you really want to overwrite?") == TRUE) {
  62.      /* Open file and write project data */
  63.      if (!(fh = Open(&path[0], (LONG)MODE_NEWFILE))) {
  64.         status = EDITOR_ERROR_OPEN_FAILED;
  65.      } else {
  66.         if ((status = write_project_header(fh, tl)) ==
  67.                              EDITOR_STATUS_NORMAL) {
  68.            if ((status = write_project_fonts(fh, tl)) ==
  69.                              EDITOR_STATUS_NORMAL) {
  70.           status = write_project_templates(fh, tl);
  71.            }
  72.         }
  73.         Close(fh);
  74.  
  75.         /* If any error then delete incomplete project file */
  76.         if (status != EDITOR_STATUS_NORMAL) {
  77.            DeleteFile(&path[0]);
  78.         } else {
  79.            tl->tl_Flags &= ~TEMPLATE_LIST_FLAG_CHANGED;
  80.         }
  81.      }
  82.       }
  83.    }
  84.    IRestoreMousePointer(ewin);
  85.    if (status != EDITOR_STATUS_NORMAL) {
  86.       show_error(status);
  87.    }
  88.    return(status);
  89. }
  90.     /* Write project header to save file */
  91.  
  92.    STATIC SHORT
  93. write_project_header(BPTR fh, struct TemplateList  *tl)
  94. {
  95.    SHORT status;
  96.  
  97.    if (FPrintf(fh, "\n\t/* Project: %s */\n\n"
  98.         "BEGIN PROJECTHEADER\n"
  99.         "   LEFTEDGE=%d\n"
  100.         "   TOPEDGE=%d\n"
  101.         "   WIDTH=%d\n"
  102.         "   HEIGHT=%d\n"
  103.         "   FLAGS=%d\n"
  104.         "   ID=\"%s\"\n"
  105.         "END PROJECTHEADER\n", &tl->tl_ProjectName[0],
  106.            pwin->LeftEdge, pwin->TopEdge, pwin->Width, pwin->Height,
  107.                    tl->tl_Flags, &tl->tl_ProjectID[0]) == -1L) {
  108.       status = EDITOR_ERROR_WRITE_FAILED;
  109.    } else {
  110.       status = EDITOR_STATUS_NORMAL;
  111.    }
  112.    return(status);
  113. }
  114.     /* Write all template fonts to save file */
  115.  
  116.    STATIC SHORT
  117. write_project_fonts(BPTR fh, struct TemplateList  *tl)
  118. {
  119.    struct TemplateFont  *tf;
  120.    USHORT count = 0;
  121.    SHORT  status = EDITOR_STATUS_NORMAL;
  122.  
  123.    for (count = 0, tf = get_head((struct List *)&tl->tl_Fonts);
  124.                   tf && status == EDITOR_STATUS_NORMAL; count++,
  125.                 tf = get_succ((struct Node *)&tf->tf_MinNode)) {
  126.       struct TextAttr  *ta = &tf->tf_TextAttr;
  127.  
  128.       if (FPrintf(fh, "\n\t/* Font %d */\n\n"
  129.         "BEGIN FONT\n"
  130.         "   NAME=\"%s\"\n"
  131.         "   YSIZE=%d\n"
  132.         "END FONT\n", count, ta->ta_Name, ta->ta_YSize) == -1L) {
  133.      status = EDITOR_ERROR_WRITE_FAILED;
  134.       }
  135.    }
  136.    return(status);
  137. }
  138.     /* Write all templates to save file */
  139.  
  140.    STATIC SHORT
  141. write_project_templates(BPTR fh, struct TemplateList  *tl)
  142. {
  143.    struct Template  *tp;
  144.    UBYTE count;
  145.    SHORT status = EDITOR_STATUS_NORMAL;
  146.  
  147.    for (count = 0, tp = get_head((struct List *)&tl->tl_Templates);
  148.                        tp && status == EDITOR_STATUS_NORMAL;
  149.                      count++, tp = get_succ(&tp->tp_Node)) {
  150.       struct Box  *box = &tp->tp_Box;
  151.       UBYTE type = tp->tp_Type;
  152.  
  153.       /* Write standard data */
  154.       if (FPrintf(fh, "\n\t/* Template %d: %s */\n\n"
  155.         "BEGIN TEMPLATE\n"
  156.         "   NAME=\"%s\"\n"
  157.         "   TYPE=%d\n"
  158.         "   FLAGS=%d\n"
  159.         "   BEGIN BOX\n"
  160.         "      X1=%d\n"
  161.         "      Y1=%d\n"
  162.         "      X2=%d\n"
  163.         "      Y2=%d\n"
  164.         "   END BOX\n", count, template_type_text_array[type],
  165.             &tp->tp_TemplateName[0], type, tp->tp_Flags, box->bo_X1,
  166.                    box->bo_Y1, box->bo_X2, box->bo_Y2) == -1L) {
  167.      status = EDITOR_ERROR_WRITE_FAILED;
  168.       } else {
  169.  
  170.      /* Write text list if any */
  171.      if (type == TEMPLATE_TYPE_MX || type == TEMPLATE_TYPE_CYCLE ||
  172.                        type == TEMPLATE_TYPE_LISTVIEW) {
  173.         if (FPrintf(fh, "   BEGIN TEXTLIST\n") == -1L) {
  174.            status = EDITOR_ERROR_WRITE_FAILED;
  175.         } else {
  176.            struct Node  *node;
  177.  
  178.            for (node = get_head(&tp->tp_TextList); node &&
  179.             status == EDITOR_STATUS_NORMAL; node = get_succ(node)) {
  180.           if (FPrintf(fh, "      TEXT=\"%s\"\n",
  181.                             node->ln_Name) == -1L) {
  182.              status = EDITOR_ERROR_WRITE_FAILED;
  183.           }
  184.            }
  185.            if (status == EDITOR_STATUS_NORMAL) {
  186.           if (FPrintf(fh, "   END TEXTLIST\n") == -1L) {
  187.              status = EDITOR_ERROR_WRITE_FAILED;
  188.           }
  189.            }
  190.         }
  191.      }
  192.  
  193.      /* Write template data */
  194.      if (status == EDITOR_STATUS_NORMAL) {
  195.         struct BorderData  *bd;
  196.         struct TextData    *td;
  197.         struct GadgetData  *gd;
  198.         BYTE *fmt;
  199.  
  200.         switch (TEMPLATE_GROUP(tp)) {
  201.            case TEMPLATE_GROUP_BORDER :
  202.  
  203.           /* Write border data */
  204.           bd = &tp->tp_Data.tp_BorderData;
  205.           if (FPrintf(fh,
  206.             "   BEGIN BORDERDATA\n"
  207.             "      TYPE=%d\n"
  208.             "   END BORDERDATA\n", bd->bd_Type) == -1L) {
  209.              status = EDITOR_ERROR_WRITE_FAILED;
  210.           }
  211.           break;
  212.  
  213.            case TEMPLATE_GROUP_TEXT :
  214.  
  215.           /* Write text data */
  216.           td = &tp->tp_Data.tp_TextData;
  217.           if (td->td_Type == TEXT_DATA_TYPE_TEXT) {
  218.              fmt = "   BEGIN TEXTDATA\n"
  219.             "      TYPE=%d\n"
  220.             "      FLAGS=%d\n"
  221.             "      TEXT=\"%s\"\n"
  222.             "      TEXTATTR=%d\n"
  223.             "   END TEXTDATA\n";
  224.           } else {
  225.              fmt = "   BEGIN TEXTDATA\n"
  226.             "      TYPE=%d\n"
  227.             "      FLAGS=%d\n"
  228.             "      TEXT=%ld\n"
  229.             "      TEXTATTR=%d\n"
  230.             "   END TEXTDATA\n";
  231.           }
  232.           if (FPrintf(fh, fmt, td->td_Type, td->td_Flags,
  233.                       td->td_Text, get_template_font_num(tl,
  234.                          td->td_TextAttr)) == -1L) {
  235.              status = EDITOR_ERROR_WRITE_FAILED;
  236.           }
  237.           break;
  238.  
  239.            case TEMPLATE_GROUP_GADGET :
  240.  
  241.              /* Write gadget data */
  242.              gd = &tp->tp_Data.tp_GadgetData;
  243.              if (FPrintf(fh,
  244.             "   BEGIN GADGETDATA\n"
  245.             "      TYPE=%ld\n"
  246.             "      FLAGS=%ld\n", gd->gd_Type,
  247.                              gd->gd_Flags) == -1L) {
  248.              status = EDITOR_ERROR_WRITE_FAILED;
  249.           } else {
  250.              if (!gd->gd_Text) {
  251.             fmt = "      TEXT=%ld\n";
  252.              } else {
  253.             fmt = "      TEXT=\"%s\"\n";
  254.              }
  255.              if (FPrintf(fh, fmt, gd->gd_Text) == -1L) {
  256.             status = EDITOR_ERROR_WRITE_FAILED;
  257.              } else {
  258.             if (gd->gd_Type == GADGET_DATA_TYPE_STRING) {
  259.                fmt = "      TEXTATTR=%d\n"
  260.                  "      SPECIAL1=%ld\n"
  261.                  "      SPECIAL2=%ld\n"
  262.                  "      SPECIAL3=\"%s\"\n";
  263.             } else {
  264.                fmt = "      TEXTATTR=%d\n"
  265.                  "      SPECIAL1=%ld\n"
  266.                  "      SPECIAL2=%ld\n"
  267.                  "      SPECIAL3=%ld\n";
  268.             }
  269.             if (FPrintf(fh, fmt, get_template_font_num(tl,
  270.                                gd->gd_TextAttr),
  271.                     gd->gd_SpecialData.gd_Data.gd_Data1,
  272.                     gd->gd_SpecialData.gd_Data.gd_Data2,
  273.                   gd->gd_SpecialData.gd_Data.gd_Data3) == -1L) {
  274.                status = EDITOR_ERROR_WRITE_FAILED;
  275.             } else {
  276.                if (FPrintf(fh, "   END GADGETDATA\n") == -1L) {
  277.                   status = EDITOR_ERROR_WRITE_FAILED;
  278.                }
  279.             }
  280.              }
  281.           }
  282.           break;
  283.         }
  284.      }
  285.      if (status == EDITOR_STATUS_NORMAL) {
  286.         if (FPrintf(fh, "END TEMPLATE\n") == -1L) {
  287.            status = EDITOR_ERROR_WRITE_FAILED;
  288.         }
  289.      }
  290.       }
  291.    }
  292.    return(status);
  293. }
  294.